package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.CarBrandMapper;
import com.gxa.pingan.pojo.CarBrand;
import com.gxa.pingan.service.CarBrandService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("carBrandService")
@Transactional(rollbackFor=Throwable.class)
public class CarBrandServiceImpl implements CarBrandService {

    @Autowired
    private CarBrandMapper carBrandMapper;


    @Override
    public void add(CarBrand carBrand) throws BusinessException {
        List<CarBrand> list = carBrandMapper.findByName(carBrand.getBrandName());
        if (list.size()!=0){
            throw new BusinessException(ExceptionCode.REPEAT_BRAND);
        }
        carBrandMapper.add(carBrand);
    }

    @Override
    public List<CarBrand> findAll() {
        List<CarBrand> all = carBrandMapper.findAll();
        return all;
    }

    @Override
    public void update(Long id, CarBrand carBrand) throws BusinessException {



        CarBrand byId = carBrandMapper.findById(id);
        if (byId!=null){
            carBrandMapper.update(carBrand);
        }

        List<CarBrand> list = carBrandMapper.findByName(carBrand.getBrandName());
        if (list.size()>=2){
            throw new BusinessException(ExceptionCode.EXIT_BRAND);
        }

    }

    @Override
    public List<CarBrand> findAll(Integer offset, Integer size) {
        return carBrandMapper.findByPage((offset-1)*size,size);
    }
}
